package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.bean.Student;
import com.hq.service.StuService;
import com.hq.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuController {

    @Resource
    private StuService stutService;

    @RequestMapping("list.do")
    public String list(@RequestParam(value="cpage",defaultValue = "1") Integer cpage,
                       String mohu1, String startTime, String endTime, Model model){
        Map map=new HashMap();
        map.put("mohu1",mohu1);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        PageHelper.startPage(cpage,3);
        List list = stutService.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("map",map);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    @RequestMapping("toadd.do")
    public String toadd(){
        return "add";
    }

    @RequestMapping("cityList.do")
    @ResponseBody
    public Object  cityList(Integer pid){
        List list=stutService.cityList(pid);
        return list;
    }

    // 课程表加载
    @RequestMapping("courseList.do")
    @ResponseBody
    public Object courseList(){
       List list= stutService.courseList();
        return list;
    }
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Student stu, MultipartFile photo, HttpSession session,Integer[] cids){
        if(photo.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            stu.setPic(upload);
        }
        int i=stutService.add(stu,cids);
        return i;
    }
}
